﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public class AxleRunState
    {
        private AxleRunState() { }
        public bool this[string axleName]
        {
            get
            {
                lock (s_tzLock)
                {
                    if (s_tzList != null && s_tzList.Count > 0 && s_tzList.ContainsKey(axleName))
                    {
                        return s_tzList[axleName];
                    }
                }
                return false;
            }
            set {
                lock (s_tzLock)
                {
                    if (s_tzList != null && s_tzList.Count > 0 && s_tzList.ContainsKey(axleName))
                    {
                        s_tzList[axleName] = value;
                    }
                }
            }
        }

        static AxleRunState m_sObj;
        static object m_oLk = new object();
        public static AxleRunState Instance
        {
            get
            {
                lock (m_oLk)
                {
                    if (m_sObj == null)
                    { m_sObj = new AxleRunState(); }
                }
                return m_sObj;
            }
        } 

        bool registed = false;
        public void Regist(string aName, AxleActions action)
        {
            if (!string.IsNullOrEmpty(aName))
            {
                List<string> aList = new List<string>();
                aList.Add(aName);
                Regist(aList, action);
            }

        }

        public void Regist(Dictionary<string, Axle> axleDict, AxleActions action)
        {
            if (axleDict != null && axleDict.Count > 0)
            {
                List<string> aList = new List<string>();
                foreach (KeyValuePair<string, Axle> a in axleDict)
                {
                    aList.Add(a.Key);
                }
                Regist(aList, action);
            }
        }

        public void Regist(List<string> axleList, AxleActions action)
        {
            if (axleList != null)
            {
                m_action = action;
                lock (s_rgLock)
                {
                    //if (!registed)
                    //{
                    //    AxleManager.OnAxleActionResponse += new AxleActionResponse(AxleManager_OnAxleActionResponse);
                    //}
                    registed = true;
                }
                lock (s_tzLock)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                foreach (string a in axleList)
                {
                    lock (s_tzLock)
                    {
                        s_tzList.Add(a, false);
                    }
                }
            }
        }

        //void AxleManager_OnAxleActionResponse(
        //    string axleName
        //    , Axle axle
        //    , AxleActions action
        //    , bool success
        //    , string message)
        //{
        //    if (action == m_action)
        //    {
        //        setActionReady(axleName, success);
        //    }
        //}

        void setActionReady(string aName, bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList != null && s_tzList.Count > 0 && s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
            }
        }

        public bool CanRun(string aName)
        {
            return !this[aName];
        }
        

        public void UnRegist()
        {
            lock (s_rgLock)
            {
                //AxleManager.OnAxleActionResponse -= new AxleActionResponse(AxleManager_OnAxleActionResponse);
                registed = false;
            }
        }
        object s_rgLock = new object();
        object s_tzLock = new object();
        Dictionary<string, bool> s_tzList;
        AxleActions m_action;
        public bool IsActionReady(string aName)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null || s_tzList.Count == 0) return false;
                if (s_tzList.ContainsKey(aName) && s_tzList[aName])
                {
                    return true;
                }
            }

            return false;
        }
        public void ResetAction(string aName)
        {
            ResetAction(aName, false);
        }
        public void ResetAction(string aName,bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                if (s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
                else {
                    s_tzList.Add(aName, ready);
                }
            }
        }

       
    }
}
